<template>
  <el-form v-bind="$attrs" @validate="formValidate">
    <el-form-item label="名称" prop="name" :rules="[{ required: true, message: '名称不能为空' }]">
      <el-input v-model="formData.name" placeholder="请输入数据标题，不超过50个字符" />
    </el-form-item>
    <el-form-item label="资源描述" prop="desc">
      <el-input
        v-model="formData.desc"
        type="textarea"
        :rows="5"
        placeholder="请输入数据的描述信息，不超过200个字符"
      />
    </el-form-item>
    <el-form-item label="缩放">
      <div class="row-item">
        <el-input
          v-model="formData.attr.scale"
          placeholder=""
          @blur="specialFormValidate('scale')"
          @keyup.enter.native="specialFormValidate('scale')"
        />
        <div class="unit">%</div>
      </div>
    </el-form-item>
    <el-form-item label="模型颜色">
      <el-color-picker v-model="formData.attr.color" @change="specialFormValidate('color')" />
    </el-form-item>
    <el-form-item label="数据坐标（wgs84）">
      <div class="row-item">
        <div class="title">经度</div>
        <el-input
          v-model="formData.attr.position[0]"
          placeholder=""
          @blur="specialFormValidate('position')"
          @keyup.enter.native="specialFormValidate('position')"
        />
        <div class="unit">°</div>
      </div>
      <div class="row-item">
        <div class="title">纬度</div>
        <el-input
          v-model="formData.attr.position[1]"
          placeholder=""
          @blur="specialFormValidate('position')"
          @keyup.enter.native="specialFormValidate('position')"
        />
        <div class="unit">°</div>
      </div>
      <div class="row-item">
        <div class="title">海拔</div>
        <el-input
          v-model="formData.attr.position[2]"
          placeholder=""
          @blur="specialFormValidate('position')"
          @keyup.enter.native="specialFormValidate('position')"
        />
        <div class="unit">米</div>
      </div>
    </el-form-item>
    <!-- <el-form-item label="模型平移">
      <div class="row-item">
        <div class="title">经度</div>
        <el-input
          v-model="formData.attr.translation[0]"
          placeholder=""
          @blur="specialFormValidate('translation')"
          @keyup.enter.native="specialFormValidate('translation')"
        />
        <div class="unit">°</div>
      </div>
      <div class="row-item">
        <div class="title">纬度</div>
        <el-input
          v-model="formData.attr.translation[1]"
          placeholder=""
          @blur="specialFormValidate('translation')"
          @keyup.enter.native="specialFormValidate('translation')"
        />
        <div class="unit">°</div>
      </div>
      <div class="row-item">
        <div class="title">海拔</div>
        <el-input
          v-model="formData.attr.translation[2]"
          placeholder=""
          @blur="specialFormValidate('translation')"
          @keyup.enter.native="specialFormValidate('translation')"
        />
        <div class="unit">米</div>
      </div>
    </el-form-item> -->
    <el-form-item label="模型旋转">
      <div class="row-item">
        <div class="title">X轴</div>
        <el-input
          v-model="formData.attr.rotation[0]"
          placeholder=""
          @blur="specialFormValidate('rotation')"
          @keyup.enter.native="specialFormValidate('rotation')"
        />
        <div class="unit">°</div>
      </div>
      <div class="row-item">
        <div class="title">Y轴</div>
        <el-input
          v-model="formData.attr.rotation[1]"
          placeholder=""
          @blur="specialFormValidate('rotation')"
          @keyup.enter.native="specialFormValidate('rotation')"
        />
        <div class="unit">°</div>
      </div>
      <div class="row-item">
        <div class="title">Z轴</div>
        <el-input
          v-model="formData.attr.rotation[2]"
          placeholder=""
          @blur="specialFormValidate('rotation')"
          @keyup.enter.native="specialFormValidate('rotation')"
        />
        <div class="unit">°</div>
      </div>
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import { messageUtil } from '@jl/element-plus/utils';

defineOptions({
  name: '',
});

interface IProps {
  form: any;
}

const props = withDefaults(defineProps<IProps>(), {
  form: {},
});

const formData = ref<any>(null);
const emits = defineEmits(['formValidate']);

function formValidate(fieldName: string, valid: boolean) {
  emits('formValidate', fieldName, valid);
}

function specialFormValidate(fieldName: string) {
  let valid = true;
  if (['position', 'translation', 'rotation'].includes(fieldName)) {
    for (const list of formData.value.attr[fieldName]) {
      if (list === '' || isNaN(list)) {
        valid = false;
      }
    }
  } else if (['scale'].includes(fieldName)) {
    if (formData.value.attr.scale === '' || isNaN(formData.value.attr.scale)) {
      valid = false;
    }
  }
  if (!valid) {
    messageUtil.warning('校验错误');
  }
  emits('formValidate', fieldName, valid);
}

watchEffect(() => {
  formData.value = props.form;
});
</script>

<style lang="less" scoped>
.el-form {
  &-item {
    .row-item {
      width: 100%;
      display: flex;
      margin-bottom: 10px;
      .title {
        flex: 0 0 40px;
      }
      .el-input {
        flex: 1;
      }
      .unit {
        flex: 0 0 24px;
        text-align: center;
      }
      &:last-child {
        margin-bottom: 0px;
      }
    }
  }
}
</style>
